package hqu.cst.xah.onlinemaill.service.imp;

import hqu.cst.xah.onlinemaill.mapper.OrderMapper;
import hqu.cst.xah.onlinemaill.pojo.Order;

import hqu.cst.xah.onlinemaill.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImp implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Order> getAllOrders() {
        return orderMapper.getAllOrders(null);
    }

    @Override
    public boolean updateOrder(Integer orderId, Order updatedOrder) {
        // 设置订单 ID，确保更新的是正确的订单
        updatedOrder.setId(orderId);

        // 调用 Mapper 更新方法
        int result = orderMapper.updateOrder(updatedOrder);

        // 如果返回值大于 0，表示更新成功
        return result > 0;
    }

    @Override
    public boolean deleteOrder(Integer orderId) {
        // 调用 Mapper 删除方法
        int result = orderMapper.deleteOrder(orderId);

        // 如果返回值大于 0，表示删除成功
        return result > 0;
    }

    @Override
    public boolean createOrder(Order newOrder) {
        try {
            int rowsAffected = orderMapper.insertOrder(newOrder);
            return rowsAffected > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
